package com.qzdsoft.erpcloud.service.act.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.qzdsoft.erpcloud.domain.act.CenterProduct;
import com.qzdsoft.erpcloud.mapper.act.CenterProductMapper;
import com.qzdsoft.erpcloud.service.act.CenterProductService;
import com.qzdsoft.erpcloud.vo.query.act.CenterProductInfo;
import com.qzdsoft.vo.LayTableResponse;
import com.qzdsoft.vo.Response;
import com.qzdsoft.vo.ResultEnum;

/**
 * Created by Yang
 */
@Service
public class CenterProductServiceImpl implements CenterProductService {

    @Autowired
    private CenterProductMapper actCenterProductMapper;

    @Override
    public CenterProduct selectById(Integer id) {
        return actCenterProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public Response<String> saveOrUpd(CenterProduct actCenterProduct) {
        
        actCenterProduct.setPeriod(actCenterProduct.getPrice()*10000);
        
        if(null != actCenterProduct.getId()){
            actCenterProductMapper.updateByPrimaryKey(actCenterProduct);
        } else {
            actCenterProductMapper.insert(actCenterProduct);
        }
        return new Response<String>(ResultEnum.SUCCESS);
    }

    @Override
    public Integer selectDataCount(String name, String type, Integer priceEdit, Integer periodType, Integer roleId) {
        return actCenterProductMapper.selectDataCount(name, type, priceEdit, periodType, roleId);
    }

    @Override
    public LayTableResponse<CenterProductInfo> selectData(String name, String type, Integer priceEdit, Integer periodType, Integer roleId, Integer start, Integer pageSize) {
        PageHelper.startPage(start, pageSize);
        List<CenterProductInfo> list = actCenterProductMapper.selectData(name, type, priceEdit, periodType, roleId, start, pageSize);
        return new LayTableResponse<CenterProductInfo>(list);
    }

    @Override
    public List<CenterProduct> selectAll() {
        return actCenterProductMapper.selectAll();
    }
    
    /**
     * 根据id删除已购买商品
     * 
     */
    public Response<String> del(Integer id) {
        actCenterProductMapper.deleteByPrimaryKey(id);
        return new Response<String>(ResultEnum.SUCCESS);
    }
}
